package com.jzo2o.customer.controller.consumer;

import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.common.utils.Base64Utils;
import com.jzo2o.common.utils.JsonUtils;
import com.jzo2o.common.utils.JwtTool;
import com.jzo2o.customer.model.domain.AddressBook;
import com.jzo2o.customer.model.dto.request.AddressBookPageQueryReqDTO;
import com.jzo2o.customer.model.dto.request.AddressBookUpsertReqDTO;
import com.jzo2o.customer.service.IAddressBookService;
import com.jzo2o.mvc.constants.HeaderConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController("AddressbookController")
@RequestMapping("/consumer/address-book")

public class AddressbookController{

    @Resource
    private IAddressBookService addressBookService;


    //consumer/address-book/page?
    // pageNo=1&pageSize=10&
    // isAsc1=false&isAsc2=false&orderBy1=isDefault&orderBy2=updateTime
    @GetMapping("/page")
    @ApiOperation("查询地址簿信息")
    public PageResult<AddressBook> findAddressBook(HttpServletRequest request,AddressBookPageQueryReqDTO addressBookPageQueryReqDTO){
//        JwtTool.getUserType("");
//        HeaderConstants.BODY_PROCESSED.equals("");
//        String userInfo = request.getHeader(com.jzo2o.common.constants.HeaderConstants.USER_INFO);
     //String decodeUserinfo =  Base64Utils.decodeStr(userInfo);
     //  CurrentUserInfo currentUserInfo =  JsonUtils.toBean(decodeUserinfo,CurrentUserInfo);
     //public class CurrentUserInfo implements Serializable {
        //    private Long id;
        //    private String name;
        //    private String avatar;
        //    private Integer userType;
        PageResult<AddressBook> pageResult =   addressBookService.findAddressBook(request,addressBookPageQueryReqDTO);

        return pageResult;
    }

    @GetMapping("/{id}")
    @ApiOperation("编辑地址回显数据")
    @ApiImplicitParam(name = "id",value = "地址簿id",required = true,dataTypeClass = Long.class)
    public AddressBook getAddressBook(@PathVariable("id") Long id){

        AddressBook addressBook = addressBookService.getAddressBook(id);
        return addressBook;
    }

//==================================="设置默认地址 1默认  0 非默认===============================
    ///default?id=1727539234670714482&flag=0

    @PutMapping("/default")
    @ApiOperation("设置默认地址 1默认  0 非默认")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "地址簿id",required = true,dataTypeClass = Long.class),
            @ApiImplicitParam(name = "flag", value = "is_default 0 || 1",required = true,dataTypeClass = int.class)
    })
    public void defaultAddress(@RequestParam("id") Long id,@RequestParam("flag") int flag){
        addressBookService.defaultAddress(id,flag);
    }

//==========================================添加地址簿信息============================================
//    address: "安徽省合肥市包河区望湖街道合肥市包河区农业综合服务中心"
//    city: "合肥市"
//    county: "包河区"
//    isDefault: 1
//    location: "117.30794,31.79322"
//    name: "ddd"
//    phone: "13333333333"
//    province: "安徽省"

    @PostMapping
    @ApiOperation("添加地址簿信息")
    public void addAddressBook(HttpServletRequest request,@RequestBody AddressBookUpsertReqDTO addressBookUpsertReqDTO){

     addressBookService.addAddressBook(request,addressBookUpsertReqDTO);
    }

//=========================================编辑地址信息更新=============================================
    //1727539234670714482
    //PUT
    @PutMapping("/{id}")
    @ApiOperation("编辑地址信息更新")
    @ApiImplicitParam(name = "id",value = "地址簿id",required = true,dataTypeClass = Long.class)
    public void updateAddress(@PathVariable("id") Long id,@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(id,addressBook);
    }
//=========================================删除地址簿信息=============================================
    //batch
    @DeleteMapping("/batch")
    @ApiOperation("删除地址")
    public void delete(@RequestBody String[] ids){
        System.out.println("ids = " + ids);
        addressBookService.delete(ids);
    }
}
